Imports MIMS_WRS.Business.Common

Public Class LookupDataInputControl
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblTitle1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblTitle2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblTitle3 As System.Web.UI.WebControls.Label
    Protected WithEvents txtValue1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtValue2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtValue3 As System.Web.UI.WebControls.TextBox
    Protected WithEvents reqvValue1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents reqvValue2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents reqvValue3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents cmpvValue1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents cmpvValue2 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents cmpvValue3 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents trTitle1 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trTitle2 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trTitle3 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents lblHeader As System.Web.UI.WebControls.Label
    Protected WithEvents regExp1 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents regExp2 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents regExp3 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
    Protected WithEvents lblRequiredText1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblRequiredText2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblRequiredText3 As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private REGULAR_EXP_STR As String = "[^'<>]{{{0},{1}}}"
    Private REGULAR_EXP_ERR_STR As String = "{2} must be from {0} to {1} character(s)"
    Private REGULAR_EXP_ERR_STR_2 As String = "{1} must be exactly {0} character(s)"
    Private REQUIRED_ERR_STR As String = "Please input {0}"
    Private COMPARE_ERR_STR As String = "Invalid input value in {0}. Must be {1} value."

#Region "   Members"

#End Region

#Region "   Properties"
    Public Property PageBOSessionName() As String
        Get
            Return CStr(ViewState("PageBOSessionName"))
        End Get
        Set(ByVal Value As String)
            ViewState("PageBOSessionName") = Value
        End Set
    End Property

    Protected Property LookupID() As Integer
        Get
            Return PageBO.LookupID
        End Get
        Set(ByVal Value As Integer)
            PageBO.LookupID = Value
        End Set
    End Property

    Protected ReadOnly Property LookupDataID() As Integer
        Get
            Return PageBO.ID
        End Get
    End Property

    Protected ReadOnly Property PageBO() As CLookupData
        Get
            Return CType(Session(Me.PageBOSessionName), CLookupData)
        End Get
    End Property
#End Region

    Private Function FromDataType2ErrMsg(ByVal type As ValidationDataType) As String
        Select Case type
            Case ValidationDataType.Currency
                Return "currency"
            Case ValidationDataType.Date
                Return "date"
            Case ValidationDataType.Double
                Return "decimal"
            Case ValidationDataType.Integer
                Return "whole number"
            Case ValidationDataType.String
                Return "alphanumerics"
        End Select
    End Function

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            Dim objLK As New CLookup
            objLK.Load(Me.LookupID)

            Try
                With objLK
                    If Me.LookupDataID > 0 Then
                        Me.lblHeader.Text = String.Format("Edit {0}", objLK.CategoryName.Replace("Destination", "Arrival"))
                    Else
                        Me.lblHeader.Text = String.Format("Create new {0}", objLK.CategoryName.Replace("Destination", "Arrival"))
                    End If

                    Me.lblTitle1.Text = .Title1
                    Me.lblTitle2.Text = .Title2
                    Me.lblTitle3.Text = .Title3

                    Me.cmpvValue1.Type = CType(.DataType1, ValidationDataType)
                    Me.cmpvValue2.Type = CType(.DataType2, ValidationDataType)
                    Me.cmpvValue3.Type = CType(.DataType3, ValidationDataType)

                    Me.cmpvValue1.ErrorMessage = String.Format(Me.COMPARE_ERR_STR, .Title1, Me.FromDataType2ErrMsg(CType(.DataType1, ValidationDataType)))
                    Me.cmpvValue2.ErrorMessage = String.Format(Me.COMPARE_ERR_STR, .Title2, Me.FromDataType2ErrMsg(CType(.DataType2, ValidationDataType)))
                    Me.cmpvValue3.ErrorMessage = String.Format(Me.COMPARE_ERR_STR, .Title3, Me.FromDataType2ErrMsg(CType(.DataType3, ValidationDataType)))

                    Me.txtValue1.MaxLength = .MaxLength1
                    Me.txtValue2.MaxLength = .MaxLength2
                    Me.txtValue3.MaxLength = .MaxLength3

                    Me.reqvValue1.Enabled = .IsRequired1
                    Me.reqvValue2.Enabled = .IsRequired2
                    Me.reqvValue3.Enabled = .IsRequired3

                    Me.lblRequiredText1.Visible = .IsRequired1
                    Me.lblRequiredText2.Visible = .IsRequired2
                    Me.lblRequiredText3.Visible = .IsRequired3

                    Me.reqvValue1.ErrorMessage = String.Format(Me.REQUIRED_ERR_STR, .Title1)
                    Me.reqvValue2.ErrorMessage = String.Format(Me.REQUIRED_ERR_STR, .Title2)
                    Me.reqvValue3.ErrorMessage = String.Format(Me.REQUIRED_ERR_STR, .Title3)

                    Me.trTitle1.Visible = Not NullOrEmpty(.Title1)
                    Me.trTitle2.Visible = Not NullOrEmpty(.Title2)
                    Me.trTitle3.Visible = Not NullOrEmpty(.Title3)

                    Me.regExp1.ValidationExpression = String.Format(Me.REGULAR_EXP_STR, .MinLength1, .MaxLength1)
                    Me.regExp2.ValidationExpression = String.Format(Me.REGULAR_EXP_STR, .MinLength2, .MaxLength2)
                    Me.regExp3.ValidationExpression = String.Format(Me.REGULAR_EXP_STR, .MinLength3, .MaxLength3)

                    If .MaxLength1 = .MinLength1 Then
                        Me.regExp1.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR_2, .MinLength1, .Title1)
                    Else
                        Me.regExp1.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR, .MinLength1, .MaxLength1, .Title1)
                    End If

                    If .MaxLength2 = .MinLength2 Then
                        Me.regExp2.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR_2, .MinLength2, .Title2)
                    Else
                        Me.regExp2.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR, .MinLength2, .MaxLength2, .Title2)
                    End If

                    If .MaxLength3 = .MinLength3 Then
                        Me.regExp3.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR_2, .MinLength3, .Title3)
                    Else
                        Me.regExp3.ErrorMessage = String.Format(Me.REGULAR_EXP_ERR_STR, .MinLength3, .MaxLength3, .Title3)
                    End If
                End With

            Catch
            End Try

            Me.DataBind()

            Page.RegisterStartupScript("focus", String.Format("<script>window.document.getElementById('{0}').focus();</script>", Me.txtValue1.ClientID))
        End If
    End Sub

#Region "   Assign BO's properties"
    Private Sub txtValue1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtValue1.TextChanged
        PageBO.Value1 = txtValue1.Text.Trim()
    End Sub

    Private Sub txtValue2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtValue2.TextChanged
        PageBO.Value2 = txtValue2.Text.Trim()
    End Sub

    Private Sub txtValue3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtValue3.TextChanged
        PageBO.Value3 = txtValue3.Text.Trim()

    End Sub
#End Region

End Class
